<?php

namespace addons\aqueue\library\jobs;

use think\Db;
use think\Log;
use think\queue\Job;

class PixelJobnew
{
    public function fire(Job $job, $data)
    {
        //执行业务处理
        Db::name('advertisers_log')->insert($data);
        if($data['ttclid'] !== 'undefined'){
            $this->manual_pixel_track($data['ttclid'],1);
        }
        $job->delete();//任务执行成功后删除
        //检查任务重试次数
        if($job->attempts() > 3){
            $job->delete();
        }
    }

    /**
     * 手动添加像素跟踪
     */
    /**
     * 手动添加像素跟踪
     */
    public function manual_pixel_track($callback,$type)
    {
        $return['code'] = 0;
        if (!empty($callback)) {
            $advertisers_auth = Db::name('advertisers_auth')->where('id',1)->find();
            if (empty($advertisers_auth)) {
                $return['msg'] = 'access_token为空';
                return $return;
            }
            $url = 'https://ads.tiktok.com/open_api/v1.2/pixel/track/';
            $data['pixel_code'] = $advertisers_auth['pixel_code'];
            $AccessToken = 'Access-Token:'.$advertisers_auth['access_token'];
            $context['ad']['callback'] = $callback;
            $data['context'] = json_encode($context);
//            浏览
            $data['event'] = 'ViewContent';
            $result = $this->posturl($url,$data,$AccessToken);
//            加购
            $data['event'] = 'AddToCart';
            $result = $this->posturl($url,$data,$AccessToken);
            //            购买
            $data['event'] = 'Checkout';
            $result = $this->posturl($url,$data,$AccessToken);
//             支付成功
            $data['event'] = 'Purchase';
            $result = $this->posturl($url,$data,$AccessToken);
            if (!empty($result)) {
                $result = json_decode($result,true);
                //日志
                $row['access_token'] = $advertisers_auth['access_token'];
                $row['pixel_code'] = $advertisers_auth['pixel_code'];
                $row['request_id'] = $result['request_id'];
                $row['ttclid'] = $callback;
                $row['type'] = $type;
                $row['createtime'] = time();
                $row['updatetime'] = time();
                Db::name('advertisers_pixel')->insert($row);
                $return['data'] = $result;
                $return['code'] = 1;
                $return['msg'] = '操作成功';
                return $return;
            } else {
                $return['msg'] = '暂无接口调用';
                return $return;
            }
        } else {
            $return['msg'] = 'ttlid为空';
            return $return;
        }
    }

    function posturl($url,$data = array(),$AccessToken=null){
        $data = (is_array($data)) ? json_encode($data,JSON_UNESCAPED_UNICODE) : $data;
        $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
        if(!empty($AccessToken)){
            $headerArray[] = $AccessToken;
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
}